<?php
include("general_include.php");
if($_SESSION['user_id']!='')
 header("location:dashboard.php");

include "top.php";
include "left.php";

$submited_ladder=array();
$submited_race=array();
$submited_rating=array();
$submited_region=array();
$submited_server=array();
$typeSql = "SELECT user_type_id FROM ".TABLEPREFIX."_user_type_user_relation WHERE user_id='".$_SESSION['new_reg_id']."' AND user_type_id!=2";
$record = $UserManagerObjAjax->GetRecords("Row",$typeSql);
$user_type = $record['user_type_id'];


if(isset($_GET['error'])) $_SESSION['error_forum'] = $_GET['error'];
if(isset($_GET['username'])) $_SESSION['forum_username'] = $_GET['username'];

if(isset($_POST['submit']))
{
   $Formval = $_POST;
   $game_id = $_POST['game_id'];
   $ladder_id = $_POST['ladder_id'];
   $server_id = $_POST['server_id']; 
   $race_id = $_POST['race_id'];
   $experience = $_POST['experience'];
   $ratevalue = $_POST['rate'];
   $lesson_plan = $_POST['lesson_plan'];
   $peak_hours = $_POST['peak_hours'];
   $need_improvement = $_POST['need_improvement'];
   $user_type = $_POST['user_type'];
 
   $paypal_email_id = $_POST['paypal_email_id'];
   $description = $_POST['description'];
   $availability_type1 = trim($_POST['availability_type1']);
   $availability_type2 = trim($_POST['availability_type2']);
   $availability_city = trim($_POST['availability_city']);
   $cityid = $_POST['cityid'];
  
   
	$ladder_id = isset($_POST['ladder_id']) ? $_POST['ladder_id'] : '0';
	$racetxt_id = isset($_POST['race_id']) && is_array($_POST['race_id']) ? implode(",",$_POST['race_id']) : '';
	$servertxt_id = isset($_POST['server_id']) && is_array($_POST['server_id']) ? implode(",",$_POST['server_id']) : '';
	$regiontxt_id = isset($_POST['region_id']) && is_array($_POST['region_id']) ? implode(",",$_POST['region_id']) : '';
	$ratingtxt_id = isset($_POST['rating_id']) && is_array($_POST['rating_id']) ? implode(",",$_POST['rating_id']) : '';
	
	// Availability Type
	  if($availability_type1!='' && $availability_type2!='')
		$availability_type = $availability_type1.','.$availability_type2;
	  if($availability_type1!='' && $availability_type2=='')   
		$availability_type = $availability_type1;
	  if($availability_type2!='' && $availability_type1=='')   
		$availability_type = $availability_type2; 
	    if($cityid!='' && $availability_type2!='') 
		 {
		   $sqlCity = "SELECT * FROM ".TABLEPREFIX."_cities WHERE city_id='$cityid'";
		   $rowCity = $UserManagerObjAjax->GetRecords("Row",$sqlCity);
		   $availability_country = $rowCity['country_id'];
		
		 }  
   
   $userSql="SELECT * FROM ".TABLEPREFIX."_user where user_id='".$_SESSION['new_reg_id']."'";
   $record = $UserManagerObjAjax->GetRecords("Row",$userSql);
   
   $Sql= "INSERT INTO ".TABLEPREFIX."_user_game set game_id='$game_id',peak_hours='$peak_hours',ladder_id='$ladder_id',server_id='$servertxt_id',race_id='$racetxt_id', 
   region_id='$regiontxt_id', rating_id='$ratingtxt_id',experience='".addslashes($experience)."',rate='$ratevalue',lesson_plan='".addslashes($lesson_plan)."',
   comment='".addslashes($comment)."',need_improvement='".addslashes($need_improvement)."',is_active='Y',user_type_id='$user_type',user_id='".$_SESSION['new_reg_id']."',
   date_added='".date("Y-m-d")."'";
   //die($Sql);
   $UserManagerObjAjax->Execute($Sql);
   
   $updateSQl = "UPDATE ".TABLEPREFIX."_user set availability_type='$availability_type',availability_city='$cityid',availability_country='$availability_country',
   paypal_email_id='$paypal_email_id',description='".addslashes($description)."' WHERE user_id='".$_SESSION['new_reg_id']."'";
   $UserManagerObjAjax->Execute($updateSQl);
   $ContactSql = "SELECT admin_email FROM ".TABLEPREFIX."_admin WHERE  admin_id=1";
   $Contact_email = $UserManagerObjAjax->GetRecords("Row",$ContactSql);
   $admin_email=$Contact_email['admin_email'];
   
   if(!mysql_error())
   {
                $tpl_file="email_templates/register_mail.html";
				$tpl_handler=fopen($tpl_file,"r");
				$tpl_message=fread($tpl_handler,filesize($tpl_file));
				fclose($tpl_handler);
			
				    $fromEmail	= "NOOBSKOOL Admin";
				    $to			= $record['email'];
			
			        $message = "Welcome to our site. You can access your account by using your email id and given password. <br /> Your login information is <br />
				    Email : ".$record['email']." and Password : ".$record['password']; 
					$url=$site_url;
					$tpl_message=str_replace("[SITEURL]",$url,$tpl_message);
					$tpl_message=str_replace("[HEADING]",$record['name'],$tpl_message);
					$tpl_message=str_replace("[MESSAGE]",$message,$tpl_message);
					
					$headers  = 'MIME-Version: 1.0' . "\r\n";
					$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
					$headers .= 'From:'.$admin_email . "\r\n" .
									'X-Mailer: PHP/' . phpversion();
					
					mail($to, 'Sucessfull Registration', $tpl_message, $headers);
			     	//unset($_SESSION['new_reg_id']);
					echo "<script>window.location.href='registration_success.php'</script>";
				    exit;  
   }

}



$gameSql = "SELECT game_id,game_name FROM ".TABLEPREFIX."_game where is_active='Y' ORDER BY game_name";
$GameArr = $UserManagerObjAjax->HtmlOptionArrayCreate($gameSql);
	
//if($Formval['country'] == '' )
//$Formval['country'] = '240';

$countrySql = "SELECT country_id,country_name FROM ".TABLEPREFIX."_country ORDER BY country_name";
$CountryArr = $UserManagerObjAjax->HtmlOptionArrayCreate($countrySql);


$smarty->assign('Formval',$Formval);
$smarty->assign('user_type',$user_type);
$smarty->assign('CountryArr',$CountryArr);
$smarty->assign('GameArr',$GameArr);
$smarty->assign('LadderArr',$LadderArr);
$smarty->assign('RaceArr',$RaceArr);
$smarty->assign('RatingArr',$RatingArr);
$smarty->assign('RegionArr',$RegionArr);
$smarty->assign('ServerArr',$ServerArr);
$smarty->assign('submited_race',$submited_race);
$smarty->assign('submited_ladder',$submited_ladder);
$smarty->assign('submited_rating',$submited_rating);
$smarty->assign('submited_region',$submited_region);
$smarty->assign('submited_server',$submited_server);
$smarty->register_modifier("inarray","in_array");
$smarty->display("registration_game.tpl");
include("footer.php");
?>